import Vue from 'vue'
import Vuex from 'vuex'
import { INCREMENT } from './mutations-types'

// 安装
Vue.use(Vuex)

// 创建对象
const store = new Vuex.Store({
  // 保存状态
  state: {
    counter: 1000,
    students: [
      {id: 110, name: 'why', age: 18},
      {id: 111, name: 'kobe', age: 24},
      {id: 112, name: 'james', age: 30},
      {id: 113, name: 'jerry', age: 10},
    ],
    info: {
      name: 'kobe',
      age: 40,
      height: 1.98
    }
  },
  // 定义方法
  mutations: {
    [INCREMENT](state) {
      state.counter++
    },
    decrement(state) {
      state.counter--
    },
    incrementCount(state, payload) {
      // console.log(count)
      // state.counter += count
      state.counter += payload.count
    },
    addStudent(state, stu) {
      state.students.push(stu)
    },
    updateInfo(state) {
      state.info.name = 'coderwhy'
    }
  },
  actions: {
    // 上下文
    aUpdateInfo(context) {
      setTimeout(() => {
        context.commit('aUpdateInfo')
      }, 1000)
    }
  },
  getters: {
    powerCounter(state) {
      return state.counter * state.counter
    },
    more20stu(state) {
      return state.students.filter(s => s.age > 20)
    },
    // getters(x,x)可以有两个值
    more20stuLength(state, getters){
      return getters.more20stu.length
    },
    moreAgeStu(state) {
      return function(age) {
        return state.students.filter(s => s.age > age)
      }
    }
  },
  // 跟新状态
  modules: {

  }
})

export default store
